import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'
import Index from './views/page/Index.vue'
import satisfaction from './views/page/satisfaction.vue'
import service from './views/page/service.vue'
import property from './views/page/property.vue'
import control from './views/page/control.vue'
import equipment from './views/page/equipment.vue'
import warehouse from './views/page/warehouse.vue'
import attendance from './views/page/attendance.vue'
import facility from './views/page/facility.vue'
import patrol from './views/page/patrol.vue'
import employee from './views/page/employee.vue'

Vue.use(Router)
let imgSrc = [
  require('./assets/images/flat/moduleTheme1x1.png'), require('./assets/images/flat/moduleTheme1x2.png'),
  require('./assets/images/flat/moduleTheme2x1.png'), require('./assets/images/flat/moduleTheme2x2.png'),
  require('./assets/images/flat/moduleTheme2x32.png'), require('./assets/images/flat/moduleTheme4x1.png'),
  require('./assets/images/flat/moduleTheme43x1.png'), require('./assets/images/flat/moduleTheme43x2.png'),
  require('./assets/images/flat/moduleTheme43x32.png'), require('./assets/images/flat/moduleTheme3x2.png'),
  require('./assets/images/default/moduleTheme1x1.png'), require('./assets/images/default/moduleTheme1x2.png'),
  require('./assets/images/default/moduleTheme2x1.png'), require('./assets/images/default/moduleTheme2x2.png'),
  require('./assets/images/default/moduleTheme2x32.png'), require('./assets/images/default/moduleTheme4x1.png'),
  require('./assets/images/default/moduleTheme43x1.png'), require('./assets/images/default/moduleTheme43x2.png'),
  require('./assets/images/default/moduleTheme43x32.png'), require('./assets/images/default/moduleTheme3x2.png')]
export default new Router({
  // mode: 'history',
  // base: '/',
  // base: process.env.BASE_URL,
  routes: [
    {
      path: '/home',
      name: 'home',
      component: Home,
      children: [
        {
          path: '/',
          name: 'index',
          component: Index,
          beforeEnter: (to, from, next) => {
            let srcList = []
            imgSrc.forEach((item) => {
              srcList.push(loadingImage(item))
            })
            Promise.all(srcList).then(res => {
              next()
            })
          }
        },
        {
          path: '/home/index',
          name: 'index',
          component: Index,
          beforeEnter: (to, from, next) => {
            let srcList = []
            imgSrc.forEach((item) => {
              srcList.push(loadingImage(item))
            })
            Promise.all(srcList).then(res => {
              next()
            })
          }
        },
        {
          path: '/home/satisfaction',
          name: 'satisfaction',
          component: satisfaction
        },
        {
          path: '/home/service',
          name: 'service',
          component: service
        },
        {
          path: '/home/property',
          name: 'property',
          component: property
        },
        {
          path: '/home/control',
          name: 'control',
          component: control
        },
        {
          path: '/home/equipment',
          name: 'equipment',
          component: equipment
        },
        {
          path: '/home/warehouse',
          name: 'warehouse',
          component: warehouse
        },
        {
          path: '/home/attendance',
          name: 'attendance',
          component: attendance
        },
        {
          path: '/home/facility',
          name: 'facility',
          component: facility
        },
        {
          path: '/home/patrol',
          name: 'patrol',
          component: patrol
        },
        {
          path: '/home/employee',
          name: 'employee',
          component: employee
        }
      ]
    }
  ]
})

function loadingImage (imgSrc) {
  return new Promise((resolve, reject) => {
    let img = new Image()
    img.src = imgSrc
    img.onload = function () {
      resolve()
    }
    img.onerror = function (e) {
      reject(e)
    }
  })
}
